package com.liu.efk.zdcq.util;



import java.io.File;
        import java.io.FileInputStream;
        import java.io.FileOutputStream;
        import sun.misc.BASE64Decoder;
        import sun.misc.BASE64Encoder;
/**
 * Created by shi860715@126.com
 * Date 2021/5/8 16:12
 * Description
 */
public class FileToBase64 {
    /**
     * <p>将文件转成base64 字符串</p>
     * @param path 文件路径
     * @return
     * @throws Exception
     */
    public static String encodeBase64File(String path) throws Exception {
        File file = new File(path);
        FileInputStream inputFile = new FileInputStream(file);
        byte[] buffer = new byte[(int)file.length()];
        inputFile.read(buffer);
        inputFile.close();
        return new BASE64Encoder().encode(buffer);
    }
    /**
     * <p>将base64字符解码保存文件</p>
     * @param base64Code
     * @param targetPath
     * @throws Exception
     */
    public static void decoderBase64File(String base64Code,String targetPath) throws Exception {
        byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
        FileOutputStream out = new FileOutputStream(targetPath);
        out.write(buffer);
        out.close();
    }
    /**
     * <p>将base64字符保存文本文件</p>
     * @param base64Code
     * @param targetPath
     * @throws Exception
     */
    public static void toFile(String base64Code,String targetPath) throws Exception {
        byte[] buffer = base64Code.getBytes();
        FileOutputStream out = new FileOutputStream(targetPath);
        out.write(buffer);
        out.close();
    }
    public static void main(String[] args) {
        try {
            String base64Code =encodeBase64File("d:/test.txt");
            System.out.println(base64Code);

            String code="DQoNCg0KDQpjdXJsIC1kICAnJyAgImh0dHA6Ly9wYW4uYmFpZHUuY29tL3Jlc3QvMi4wL3hwYW4v\n" +
                    "ZmlsZT9tZXRob2Q9cHJlY3JlYXRlJmFjY2Vzc190b2tlbj0xMjEuZDUzN2M1NTU0YjAyNmFhYjQ4\n" +
                    "NDhmYjFjNDllN2Y3MDUuWWc3NlBCajdxVy1hdks0dXJJeFNfcG1OVkFPOF9uVldXXzA5UTJwLkdj\n" +
                    "UWlpdyZwYXRoPS9hcHBzLzEyMy/pl67popgyLnBuZyZydHlwZT0zJnNsaWNlLW1kNT1mZWZmODI2\n" +
                    "NTgxMWFjY2UwNzMzNzY1MmQ1OTc2MTQ0MyZzaXplPTE5NzI2MiZhdXRvaW5pdD0xJmNvbnRlbnQt\n" +
                    "bWQ1PWZlZmY4MjY1ODExYWNjZTA3MzM3NjUyZDU5NzYxNDQzJmJsb2NrX2xpc3Q9WyJmZWZmODI2\n" +
                    "NTgxMWFjY2UwNzMzNzY1MmQ1OTc2MTQ0MyJdJmlzZGlyPTAiDQoNCg0KDQoNCg0KDQoNCg0KDQoN\n" +
                    "CmN1cmwgLWQgICAgJ3BhdGg9L2FwcHMvMTIzLzE2MDUwNzQwOTAyMDcucG5nXCZydHlwZT0zXCZz\n" +
                    "bGljZS1tZDU9ZTA1ZDU2OThjNjU4N2JjYmQ5Y2NiODMzNGI1NmE1M2Qmc2l6ZT0zOTgyMyZhdXRv\n" +
                    "aW5pdD0xXCZjb250ZW50LW1kNT1lMDVkNTY5OGM2NTg3YmNiZDljY2I4MzM0YjU2YTUzZFwmYmxv\n" +
                    "Y2tfbGlzdD1bImUwNWQ1Njk4YzY1ODdiY2JkOWNjYjgzMzRiNTZhNTNkIl1cJmlzZGlyPTAnICAg\n" +
                    "Imh0dHA6Ly9wYW4uYmFpZHUuY29tL3Jlc3QvMi4wL3hwYW4vZmlsZT9tZXRob2Q9cHJlY3JlYXRl\n" +
                    "JmFjY2Vzc190b2tlbj0xMjEuZDUzN2M1NTU0YjAyNmFhYjQ4NDhmYjFjNDllN2Y3MDUuWWc3NlBC\n" +
                    "ajdxVy1hdks0dXJJeFNfcG1OVkFPOF9uVldXXzA5UTJwLkdjUWlpdyINCg0Kb2sNCmxpbnV4DQpj\n" +
                    "dXJsIC1kICAncGF0aD0vYXBwcy8xMjMv6Zeu6aKYMi5wbmcmcnR5cGU9MyZzbGljZS1tZDU9ZmVm\n" +
                    "ZjgyNjU4MTFhY2NlMDczMzc2NTJkNTk3NjE0NDMmc2l6ZT0xOTcyNjImYXV0b2luaXQ9MSZjb250\n" +
                    "ZW50LW1kNT1mZWZmODI2NTgxMWFjY2UwNzMzNzY1MmQ1OTc2MTQ0MyZibG9ja19saXN0PVsiZmVm\n" +
                    "ZjgyNjU4MTFhY2NlMDczMzc2NTJkNTk3NjE0NDMiXSZpc2Rpcj0wJyAgImh0dHA6Ly9wYW4uYmFp\n" +
                    "ZHUuY29tL3Jlc3QvMi4wL3hwYW4vZmlsZT9tZXRob2Q9cHJlY3JlYXRlJmFjY2Vzc190b2tlbj0x\n" +
                    "MjEuZDUzN2M1NTU0YjAyNmFhYjQ4NDhmYjFjNDllN2Y3MDUuWWc3NlBCajdxVy1hdks0dXJJeFNf\n" +
                    "cG1OVkFPOF9uVldXXzA5UTJwLkdjUWlpdyINCg0KDQoNCg0KDQpjdXJsIC1kICAgIOKAnXBhdGg9\n" +
                    "L2FwcHMvMTIzLzE2MDUwNzQwOTAyMDcucG5nJnJ0eXBlPTMmc2xpY2UtbWQ1PWUwNWQ1Njk4YzY1\n" +
                    "ODdiY2JkOWNjYjgzMzRiNTZhNTNkJnNpemU9Mzk4MjMmYXV0b2luaXQ9MSZjb250ZW50LW1kNT1l\n" +
                    "MDVkNTY5OGM2NTg3YmNiZDljY2I4MzM0YjU2YTUzZCZibG9ja19saXN0PVsiZTA1ZDU2OThjNjU4\n" +
                    "N2JjYmQ5Y2NiODMzNGI1NmE1M2QiXSZpc2Rpcj0wIiAgICJodHRwOi8vcGFuLmJhaWR1LmNvbS9y\n" +
                    "ZXN0LzIuMC94cGFuL2ZpbGU/bWV0aG9kPXByZWNyZWF0ZSZhY2Nlc3NfdG9rZW49MTIxLmQ1Mzdj\n" +
                    "NTU1NGIwMjZhYWI0ODQ4ZmIxYzQ5ZTdmNzA1LllnNzZQQmo3cVctYXZLNHVySXhTX3BtTlZBTzhf\n" +
                    "blZXV18wOVEycC5HY1FpaXci";
           decoderBase64File(code, "d:/123.txt");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}